const cryptoJs = require('crypto-js')

var qtTime = [83, 80, 56, 56, 58];
var cookieToken = [83, 80, 54, 58];
var tokenStr = [115, 119, 112, 99, 116, 97, 99, 114, 107, 97, 118, 113, 109, 103, 112];
let cookie = 'QN1=0000f9002f1061b1a1f03eb9; QN48=afb67be0-bce1-4d92-83e9-4dc7106b2c02; F234=1719643710555; QN300=organic; ctt_june=1683616182042##iK3waStwaUPwawPwasWhWsvmWsHIaRj%3DaKTRVRD8W%3DX%3DX%3DGDX%3DamX2PmWRfTiK3siK3saKgnVKj%3DasgnaKj%2BVuPwaUvt; Alina=b43841b2-cb8e33-3742a330-69a02240-34aa667fae40; quinn=5ec06f07c38934363b49b508c6865a071cf2ea90da1a1bac551129c2b8fed1e29c009a375f00907caca8dae130aba251; QN668=51%2C57%2C51%2C59%2C56%2C54%2C53%2C57%2C57%2C58%2C53%2C54%2C51; QN621=fr%3Dtouch_index_search; RT="z=1&dm=m.flight.qunar.com&si=e9dd749f-1c9d-403e-ba19-cdf2458c9ae3&ss=lxzrgr1l&sl=1&tt=1fn&rl=1"; ctf_june=1683616182042##iK3waSj8WhPwawPwasD%3Da2DAVKihaDGRXsgnER3NW2GIWRXwW2kDXS3AWKgNiK3siK3saKgnVKj%3DWRa%2BVKXOWUPwaUvt; cs_june=49a88f0dfa80248233f19bacf17fd131d1fa36c0f71714d0c5e483f7d4c1f1dbb625fe6326e3e1ac3fabf8839818415662f8024fa4617ce275d10d71e312bc1cb17c80df7eee7c02a9c1a6a5b97c1179e7d1a26d8a437af52e3999a459eed3ea5a737ae180251ef5be23400b098dd8ca; F235=1719644643395';
function get_data_from_cookie(e, t) {
    function n(e, t) {
        e = e || {};
        for (var n in t)
            void 0 === e[n] && (e[n] = t[n]);
        return e
    }

    function i(e) {
        var t = e;
        return t instanceof Date || (t = new Date,
            t.setTime(t.getTime() + 1e3 * e)),
            t.toUTCString()
    }

    var r;
    if (r = n(r, {
        expires: 31536e3,
        path: "/",
        secure: 1, //"https:" === window.location.protocol,
        nulltoremove: !0,
        autojson: !0,
        autoencode: !0,
        encode: function (e) {
            return encodeURIComponent(e)
        },
        decode: function (e) {
            return decodeURIComponent(e)
        },
        error: function (e, t, n) {
            throw new Error(e)
        },
        fallback: !1
    }),
        t = n(t, r),
    "string" == typeof e) {
        var s = cookie.split(/;\s*/).map(t.autoencode ? t.decode : function (e) {
                return e
            }
        ).map(function (e) {
            return e.split("=")
        }).reduce(function (e, t) {
            return e[t[0]] = t.splice(1).join("="),
                e
        }, {})[e];
        if (!t.autojson)
            return s;
        var o;
        try {
            o = JSON.parse(s)
        } catch (u) {
            o = s
        }
        return "undefined" == typeof o && t.fallback && (o = t.fallback(e, t)),
            o
    }
    for (var a in e) {
        var f = e[a]
            , l = "undefined" == typeof f || t.nulltoremove && null === f
            , c = t.autojson ? JSON.stringify(f) : f
            , h = t.autoencode ? t.encode(c) : c;
        l && (h = "");
        var p = t.encode(a) + "=" + h + (t.expires ? ";expires=" + i(l ? -1e4 : t.expires) : "") + ";path=" + t.path + (t.domain ? ";domain=" + t.domain : "") + (t.secure ? ";secure" : "");
        t.test && t.test(p),
            document.cookie = p;
        var d = r(t.encode(a)) || "";
        f && !l && t.expires > 0 && JSON.stringify(d) !== JSON.stringify(f) && (navigator.cookieEnabled ? t.fallback ? t.fallback(e, t) : t.error("Cookie too large at " + f.length + " characters") : t.error("Cookies not enabled"))
    }
    return r
}

function encrypt_sha1(text){
    return cryptoJs.SHA1(text)
}

function MD5(text){
    return cryptoJs.MD5(text)
}
function getQtTime(t) {
    return t ? Number(t.split(",").map(function (e) {
        return String.fromCharCode(e - 2)
    }).join("")) : 0
}

function dencryptCode(t) {
    return t.map(function (e) {
        return String.fromCharCode(e - 2)
    }).join("")
}

function encryptFunction() {
    return [function (e) {
        // u.default = encrypt_sha1
        var t = encrypt_sha1(e).toString();
        // f.default = MD5
        return MD5(t).toString()
    }
        , function (e) {
            var t = MD5(e).toString();
            return encrypt_sha1(t).toString()
        }
    ]
}

function encryptToken(t) {
    return MD5(t).toString()
}

function getTokenStr() {
    let key = dencryptCode(cookieToken) // 'QN48'
    let rst = get_data_from_cookie(key)
    return rst
}

function encrypt() {
    let t = getTokenStr()
        , key = dencryptCode(qtTime) // 'QN668'
        , data = get_data_from_cookie((key))
        , n = getQtTime(data)
        , s = n % 2;
    return encryptFunction()[s](t + n)
}

function getRandomKey(t) {
    var n = "";
    var r = ("" + t).substr(4);
    r.split("").forEach(function (e) {
        n += e.charCodeAt()
    });
    var i = MD5(n).toString();
    return i.substr(-6)
}

// headers加密键值对
function getToken() {
    var t = {};
    key = dencryptCode(qtTime) // 'QN668'
    data = get_data_from_cookie((key))
    time = getQtTime(data)
    t[getRandomKey(time)] = encrypt();
    return t
}

// 载荷__m__值生成
function get_m() {
    return encryptToken(encrypt())
}

// headers['pre']生成
var i = window._pt_ ? window._pt_.slice(0, 46) : "";
headers['pre'] = i

console.log(getToken())
console.log(get_m())
